import openanything
from __init__ import FORMAT_TYPES, UnknownMicroformatError, MicroformatNotFoundError

class ValidatorResult(object):
    def __init__(self, property, message):
        self.property = property
        self.message = message

class ValidatorResults(object):
    def __init__(self):
        self.results = {}
        self.iterable = None
    
    def __iter__(self):
        if self.iterable == None:
            self.iterable = ValidatorResultsIterator()
            for key in self.results.keys():
                self.iterable.add(key, self.results[key])
        return self.iterable
    
    def __len__(self):
        count = 0
        for key in self.results.keys():
            count += len(self.results[key])
        return count
    
    def add(self, property, message):
        if property not in self.results:
            self.results[property] = []
        self.results[property].append(message)
    
    def get_results(self):
        return self.results
    
    def message(self):
        msg = ""
        for prop in self.results:
            msg = "%s: %s; " % (prop, self.results[prop])
        return msg
    
    def next(self):
        return value
    
    def passed(self):
        return len(self.results.keys()) == 0

class ValidatorResultsIterator(object):
    """
    The iterator for validator results
    
    Note that when you access the results of a validation in this manner, each
    message will pertain to a single property within the microformat in question. If 
    you want to access all of the messages for a particular property, access them via
    ValidatorResults.results.
    """
    def __init__(self):
        self.results = []
        self.pointer = 0
    
    def __iter__(self):
        return self
    
    def add(self, property, messages):
        """
        Add multiple messages for a particular property
        
        Arguments:
            property    The mf property name
            messages    A list of the messages for a particular property
        """
        for message in messages:
            self.results.append(ValidatorResult(property, message))
    
    def next(self):
        if len(self.results) <= self.pointer: raise StopIteration
        value = self.results[self.pointer]
        self.pointer += 1
        return value


def validate(data, validate_type):
    """
    Validate the string against a particular microformat
    """
    # check if the type is a valid microformat
    if not FORMAT_TYPES.count(validate_type):
        raise UnknownMicroformatError('"%s" is not a understood microformat' % validate_type)
    
    # import the library
    format = __import__(validate_type)
    
    return format.validate(data)

def validate_url(url, validate_type="all"):
    """
    Validate the given URL.
    
    Type can be the name of a supported microformat or "all".
    """
    # get the URL resource
    opened_url = openanything.fetch(url, agent=microformats.VALIDATOR_USER_AGENT)
    
    # parse it for each type
    if validate_type == "all":
        results = []
        for format in microformats.format_types:
            results.append(validate(url))
    else:
        # validate against one type
        results = [validate(url, validate_type)]